package com.burgeon.backend.service.Impl;


import com.burgeon.backend.Rest.Tags.FindTagsIdByVip;
import com.burgeon.backend.Rest.Tags.FindTagsItem;
import com.burgeon.backend.Rest.Tags.InsertVipTagsBos;
import com.burgeon.backend.Rest.Tags.ModVipTagsBos;
import com.burgeon.backend.Rest.User.ModVipDataBos;
import com.burgeon.backend.Rest.coupon.util.CreateIntegral;
import com.burgeon.backend.entity.*;
import com.burgeon.backend.mappers.AccountMapper;
import com.burgeon.backend.mappers.ComplementMapper;
import com.burgeon.backend.service.ComplementService;
import com.burgeon.backend.service.ex.ErrorDataException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Arrays;

@Service
public class ComplementServiceImpl implements ComplementService {

    private final static Log logger = LogFactory.getLog(ComplementService.class);

    @Autowired
    private ComplementMapper complementMapper;
    @Autowired
    private AccountMapper accountMapper;

    @Override
    public void makeUserTags(String phone, VipAttrib vipAttrib,String cid, String token) throws ErrorDataException {
        Info info = accountMapper.GetTokenData(token);
        FindTagsIdByVip findTagsIdByVip = new FindTagsIdByVip(info.getUrl(), info.getAccount(), info.getPassword());
        ModVipTagsBos modVipTagsBos = new ModVipTagsBos(info.getUrl(), info.getAccount(), info.getPassword());
        InsertVipTagsBos insertVipTagsBos = new InsertVipTagsBos(info.getUrl(), info.getAccount(), info.getPassword());

        try {
            modVipTagsBos.getData(findTagsIdByVip.getData(phone), vipAttrib);
        } catch (Exception e) {

            try {
                insertVipTagsBos.getData(phone, vipAttrib);
            } catch (Exception ex) {
                logger.error(e.getMessage()+"添加vip标签错误");

                throw new ErrorDataException("添加vip标签错误");
            }
        }
        //记录为null
        if (complementMapper.findVipRecord(cid,phone)==null){
            //创建记录
            ComplementRecord complementRecord = new ComplementRecord();
            complementRecord.setCode(cid);
            complementRecord.setUser(phone);
            complementMapper.addComplementRecord(complementRecord);
            //加积分
            String award = complementMapper.findAward(cid);
            CreateIntegral createIntegral = new CreateIntegral(info.getUrl(), info.getAccount(), info.getPassword());
            try {
                createIntegral.getData(phone,"-"+award);
            } catch (Exception e) {
                logger.error(e.getStackTrace());
                try {
                    createIntegral.getData(phone,award);
                } catch (Exception ex) {
                    logger.error("two"+ Arrays.toString(e.getStackTrace()));
                }
            }
        }

    }

    @Override
    public ArrayList<String> getTagsItem(String code, String token) {
        Info info = accountMapper.GetTokenData(token);
        FindTagsItem findTagsItem = new FindTagsItem(info.getUrl(),info.getAccount(),info.getPassword());
        try {
           return findTagsItem.getData(code);
        } catch (Exception e) {
            logger.error(e.getMessage()+ Arrays.toString(e.getStackTrace()));
            return null;
        }

    }

    @Override
    public void delComplement(String id) throws ErrorDataException {
       try {
           complementMapper.delComplement(id);
       }catch (Exception e){
           logger.error(e.getStackTrace());
           throw new ErrorDataException("出现错误");
       }
    }

    @Override
    public void newComplementSwitch(Complement complement) throws ErrorDataException {
        try {
            complementMapper.addComplementSwitch(complement);
        }catch (Exception e){
            logger.error(e.getStackTrace());
            throw new ErrorDataException("创建失败,类型重复");
        }
    }

    @Override
    public ArrayList<Complement> getComplementSwitch(String phone,String account) throws ErrorDataException {

        ArrayList<Complement> data = complementMapper.showComplementSwitch(account);
        ArrayList<String> rec = complementMapper.findRecord(phone);

        for (Complement datum : data) {
            for (String s : rec) {
                if (datum.getId().equals(s)) {
                    datum.setUsa("Y");
                }
            }
        }

        return data;
    }

    @Override
    public ArrayList<Complement> getComplement(String account) throws ErrorDataException {

        return complementMapper.showComplementSwitch(account);
    }

    @Override
    public void modVipData(String token, User user, String id) throws ErrorDataException {
        Info info = accountMapper.GetTokenData(token);//toto
        ModVipDataBos modVipDataBos = new ModVipDataBos(info.getUrl(),info.getAccount(),info.getPassword());
        try {
            modVipDataBos.getData(id,user);
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new ErrorDataException("出现错误");
        }

    }
}
